import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import url from "@rollup/plugin-url";
import terser from "@rollup/plugin-terser";
import html from "@rollup/plugin-html";
import fs from "fs";
const isProduction = process.env.NODE_ENV === "production";
const customTemplate = fs.readFileSync("./index.html", "utf-8");

export default {
  input: "index.js",
  // external: ['lodash'], // 不打包xx
  output: {
    dir: "./dist",
    format: "es",
    banner: "/* my-library version " + "1.0.0" + " */",
    footer: "/* follow me on Twitter! @rich_harris */",
    // chunkFileNames: "[name]-[hash].js",
    // assetFileNames: "[name]-[hash][extname]",
    manualChunks(id) {
      if (id.includes("lodash")) {
        return "vendor/lodash"; // 将 lodash 相关模块单独打包
      }
    },
    minifyInternalExports: true, //是否将导出的变量名混淆
    generatedCode: {
      constBindings: {
        inline: true,
      },
    },
    sourcemap: {
      sourcemapDebugIds:true
    },
  },
  cache: true, //默认开启的， Rollup 将只会对改变的模块重新分析，从而加速观察模式中后续的构建。
  plugins: [
    html({
      template: () => {
        return customTemplate;
      },
    }),
    resolve(),
    commonjs(),
    url({
      include: ["**/*.png", "**/*.jpg", "**/*.jpeg"], // 处理的图片类型
      fileName: "assets/[name]-[hash][extname]", // 相对 destDir 的路径：dist/[name]-[hash][extname]
      destDir: "dist",
    }),
    isProduction &&
      terser({
        compress: {
          drop_console: true, // 移除 console 语句
          dead_code: true, // 移除未使用的代码
          pure_funcs: ["Math.log"], // 移除特定函数调用
        },
        mangle: {
          toplevel: true, // 混淆顶级变量名
          properties: {
            regex: /^_/, // 只混淆以下划线开头的属性
          },
        },
        format: {
          comments: false, // 移除所有注释
          beautify: false, // 不美化代码（默认）
        },
        ecma: 2020, // 指定 ECMAScript 版本
        keep_classnames: false, // 保留类名
        keep_fnames: false, // 保留函数名
      }),
  ],
};
